import * as upms from './upms'

class ApiList {
  constructor(Vue, options) {
    this.baseUrl = options.baseUrl
    this.rewriteList = [upms]
    this.init(Vue, options)
  }
  rewritePath (list) {
    for (let key in list) {
      const item = list[key]
      for (let childKey in item) {
        if (this.baseUrl) {
          item[childKey] = this.baseUrl + item[childKey]
        } else {
          item[childKey] = item[childKey]
        }
      }
    }
  }
  rewritePathList (list) {
    list.forEach((path) => {
      this.rewritePath(path)
    })
  }
  setWin (Vue) {
    window.$api = Vue.prototype.$api = this.apiList
  }
  init (Vue, options) {
    this.rewritePathList(this.rewriteList)
    this.apiList = {
      upms,
      ...options.apiList
    }
    this.setWin(Vue)
  }
}
export default {
  install (Vue, options) {
    new ApiList(Vue, options)
  }
} 
